//
//  UIViewController+KSNavigationItem.m
//  BasketBallLeagues
//
//  Created by kenshin on 4/23/17.
//  Copyright © 2017 kenshin. All rights reserved.
//

#import "UIViewController+KSNavigationItem.h"

@implementation UIViewController (KSNavigationItem)

- (UIButton *)ks_setupLeftBackBarButtonItem
{
    UIButton *button = [self buttonWithTitle:nil
                                       image:[[UIImage imageNamed:@"navigation_back_icon"] jc_imageWithColor:[UIColor whiteColor]]
                            highLightedImage:nil];
    [button addTarget:self
               action:@selector(ks_leftBackButtonPressed:)
     forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    return button;
}

- (UIButton *)ks_setupLeftBackBarButtonItemWithColor:(UIColor *)color
{
    UIButton *button = [self buttonWithTitle:nil
                                       image:[[UIImage imageNamed:@"navigation_back_icon"] jc_imageWithColor:color]
                            highLightedImage:nil];
    [button addTarget:self
               action:@selector(ks_leftBackButtonPressed:)
     forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    return button;
}

- (void)ks_leftBackButtonPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIButton *)ks_setupLeftButtonItemWithTitle:(NSString *)title
                                        image:(UIImage *)image
                             highLightedImage:(UIImage *)highLightedImage
{
    UIButton *button = [self buttonWithTitle:title
                                       image:image
                            highLightedImage:highLightedImage];
    [button addTarget:self
               action:@selector(ks_leftButtonPressed:)
     forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    return button;
}

- (void)ks_leftButtonPressed:(id)sender
{
    
}

- (UIButton *)ks_setupRightButtonItemWithTitle:(NSString *)title
                                         image:(UIImage *)image
                              highLightedImage:(UIImage *)highLightedImage
{
    UIButton *button = [self buttonWithTitle:title
                                       image:image
                            highLightedImage:highLightedImage];
    [button addTarget:self
               action:@selector(ks_rightButtonPressed:)
     forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    return button;
}

- (void)ks_rightButtonPressed:(id)sender
{
    
}

#pragma mark -

- (UIButton *)buttonWithTitle:(NSString *)title
                        image:(UIImage *)image
             highLightedImage:(UIImage *)highLightedImage
{
    UIButton *button = [[UIButton alloc] init];
    title = [title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (title.length > 0) {
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:0.91 green:0.2 blue:0.24 alpha:1] forState:UIControlStateNormal];
        [button setTitleColor:[[UIColor colorWithRed:0.91 green:0.2 blue:0.24 alpha:1] colorWithAlphaComponent:0.7f] forState:UIControlStateDisabled];
        button.titleLabel.font = [UIFont systemFontOfSize:16.f];
    }
    if (image) {
        if (!highLightedImage) {
            highLightedImage = image;
        }
        [button setImage:image forState:UIControlStateNormal];
        [button setImage:highLightedImage forState:UIControlStateHighlighted];
    }
    [button sizeToFit];
    return button;
}

@end
